<!DOCTYPE html>

<html xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>系统用户列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">

  </head>
  
  <body>
    
    <div class="x-body">
      <!--表格-->
      <table id="demo" lay-filter="test"></table>
    </div>

    <!--toolbar :头部工具栏-->
    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
      </div>
    </script>

    <!--tool :右侧工具栏-->
    <script type="text/html" id="barDemo">
      <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <!--开关-->
    <script type="text/html" id="switchTpl">
      <input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|未启用" lay-filter="sexDemo" {{ d.status == 1 ? 'checked' : '' }}>
    </script>

<script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="api/baseApi.js" charset="utf-8"></script>
<script th:inline="none">
      layui.use(['laydate','table','form'], function(){
        var laydate = layui.laydate
        ,table = layui.table
        ,form = layui.form
        ,$ = layui.$;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });

        //第一个实例
        table.render({
          elem: '#demo'
          ,cellMinWidth: 80 //宽度自适应
          ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
          ,defaultToolbar: ['filter', 'exports', 'print']//自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
          ,url: baseUrl + sysuserBaseUrl + view + 'userList'  //数据接口
          ,page: true //开启分页
          ,limit: 10
          ,limits: [10,20,30]
          ,cols: [[ //表头
            {title: '序号',type: "numbers",align: "center",width: "5%" },
            {field: 'id',title: 'ID',align: "center",width: "5%"},
            {field: 'name',title: '姓名',align: "center",width: "10%"},
            {field: 'sex',title: '性别',align: "center",width: "5%",templet: function(d) {
                return d.sex == 'M' ? '男' : '女';
              },
            },
            {field: 'introduce',title: '简介',align: "center",width: "18%"},
            {field: 'telephone',title: '手机号',align: "center",width: "10%"},
            {field: 'email',title: '邮箱',align: "center",width: "12%"},
            {field: 'lastLoginTime',title: '最后登录时间',align: "center",width: "12%"},
            {field: 'status', title: '状态',align:'center',templet: '#switchTpl',width:"8%"},
            {fixed: 'right',title: '操作', align:'center', toolbar: '#barDemo'}
          ]],
          text: {
            none: '暂无相关数据或权限不够'
          }
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'add':
              var data = checkStatus.data;
              //layer.alert(JSON.stringify(data));
              let index = layer.open({
                title:'添加管理员用户',
                type:2,
                shade:0.2,
                maxmin:true,
                shadeClose:true,
                area:['100%','100%'],
                content:'sysuserAdd',
                yes: function(index, layero){
                  parent.location.reload(); // 父页面刷新
                  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                  parent.layer.close(index);
                }
              })
              break;
          };
        });

        //监听状态开关操作
        form.on('switch(sexDemo)', function(obj){
         // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
          var id = this.value; // 当行ID
          var status = this.checked ? '1' : '0'; // 选中状态
          var data ={id,status}
          console.log(data)
          $.ajax({
            url: baseUrl + sysuserBaseUrl + edit +"status",
            type:'put',
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success:function (resp) {
              if(resp.code == '200'){
                layer.msg(obj.elem.checked ? "该用户已被冻结" : "解除冻结")
              }else{
                layer.msg(resp.msg)
              }
            }
          })
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
          var data = obj.data;
          //console.log(obj)
          if(obj.event === 'del'){
            layer.confirm('真的删除行么', function(index){
              //console.log(obj.data)
              obj.del();
              $.ajax({
                url: baseUrl + sysuserBaseUrl + del + data.id,
                type:'delete',
                success:function(res){
                  layer.msg(res.msg)
                  table.reload('demo')
                },
                error:function(res){
                  layer.msg(res.msg)
                }
              })
              layer.close(index);
            });
          } else if(obj.event === 'edit'){
            //将数据存入本地sessionData中
            layui.sessionData('sysuserData',{
              key:'sysuser'
              ,value:data
            })
            let index = layer.open({
              title:'管理员用户编辑',
              type:2,
              shade:0.2,
              maxmin:true,
              shadeClose:true,
              area:['100%','100%'],
              content:'sysuserEdit',
              yes: function(index, layero){
                parent.location.reload(); // 父页面刷新
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
              }
            })
          }
        });


      });
</script>
<script>var _hmt = _hmt || []; (function() {
    var hm = document.createElement("script");
    hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(hm, s);
  })();</script>
</body>
</html>